package com.ldl.exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ldl.exam.constant.Constants;
import com.ldl.exam.dao.StuMapper;
import com.ldl.exam.entity.Stu;
import com.ldl.exam.entity.StuExample;
import com.ldl.exam.exception.ExamException;
import com.ldl.exam.service.StuService;
import com.ldl.exam.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liudaolin
 * @version 1.0
 * @date 2022/10/02 21:29:47
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class StuServiceImpl implements StuService {
    @Autowired
    private StuMapper stuMapper;

    @Transactional(readOnly = true)
    @Override
    public Stu login(String loginName, String pwd) throws ExamException {
        StuExample example = new StuExample();
        example.or()
                .andLoginNameEqualTo(loginName)
                .andPwdEqualTo(CommonUtil.md5ToBase64(pwd));// 加密
        List<Stu> list = stuMapper.selectByExample(example);
        if(list.isEmpty()) {
            throw new ExamException("登录名或密码错误");
        }
        Stu stu = list.get(0);
        Integer status = stu.getStatus();
        if(status.equals(Constants.STU_AUDITING) || status.equals(Constants.STU_DELETED)){
            throw new ExamException("账号状态异常,不能登录");
        }
        return stu;
    }

    @Override
    public void register(Stu stu, String rePwd) throws ExamException {
        if( !stu.getPwd().equals(rePwd) ) {
            throw new ExamException("两次输入的密码不一致");
        }
        StuExample example = new StuExample();
        if(!CommonUtil.isEmpty(stu.getLoginName())) {
            example.or()
                    .andLoginNameEqualTo(stu.getLoginName());
            List<Stu> list = stuMapper.selectByExample(example);
            if (!list.isEmpty()) {
                throw new ExamException("登录名和其他学生重复");
            }
        }
        if(!CommonUtil.isEmpty(stu.getIdCard())){
            example.or()
                    .andIdcardEqualTo(stu.getIdCard());
            List<Stu> list = stuMapper.selectByExample(example);
            example.clear();
            if(!list.isEmpty()) {
                throw new ExamException("身份证号码和其他学生重复");
            }
        }
        if(!CommonUtil.isEmpty(stu.getPhone())) {
            example.or()
                    .andPhoneEqualTo(stu.getPhone());
            List<Stu> list = stuMapper.selectByExample(example);
            if(!list.isEmpty()) {
                throw new ExamException("手机号码和其他学生重复");
            }
        }
        // 加密
        stu.setPwd(CommonUtil.md5ToBase64(stu.getPwd()));
        stuMapper.insertSelective(stu);
    }

    @Override
    public void modifyStuInfo(Stu stu) throws ExamException {
        // idCard, phone 不能和其他 stu 重复
        StuExample example = new StuExample();
        if(!CommonUtil.isEmpty(stu.getIdCard())){
            example.or()
                    .andIdcardEqualTo(stu.getIdCard())
                    .andIdNotEqualTo(stu.getId());
            List<Stu> list = stuMapper.selectByExample(example);
            example.clear();
            if(!list.isEmpty()) {
                throw new ExamException("身份证号码和其他学生重复");
            }
        }

        if(!CommonUtil.isEmpty(stu.getPhone())) {
            example.or()
                    .andPhoneEqualTo(stu.getPhone())
                    .andIdNotEqualTo(stu.getId());
            List<Stu> list = stuMapper.selectByExample(example);
            if(!list.isEmpty()) {
                throw new ExamException("手机号码和其他学生重复");
            }
        }
        stuMapper.updateByPrimaryKeySelective(stu);
    }

    @Override
    public void modifyPwd(Integer stuId, String oldPwd, String newPwd, String rePwd) throws ExamException {
        if( null == newPwd || !newPwd.equals(rePwd)){
            throw new ExamException("两次输入的新密码不一致");
        }

        StuExample example = new StuExample();
        example.or()
                .andIdEqualTo(stuId)
                .andPwdEqualTo(CommonUtil.md5ToBase64(oldPwd));
        List<Stu> list = stuMapper.selectByExample(example);
        if(list.isEmpty()) {
            throw new ExamException("旧密码输出错误");
        }

        Stu stu = new Stu();
        stu.setId(stuId);
        stu.setPwd(CommonUtil.md5ToBase64(newPwd));
        stuMapper.updateByPrimaryKeySelective(stu);
    }

    @Override
    public PageInfo<Stu> findByPage(int pageNum, int pageSize, StuExample example) {
        PageHelper.startPage(pageNum, pageSize);
        List<Stu> list = stuMapper.selectByExample(example);
        return new PageInfo<>(list, 5);
    }

    @Override
    public void modifyStatusById(Integer stuId, Integer status) {
        Stu stu = new Stu();
        stu.setId(stuId);
        stu.setStatus(status);
        stuMapper.updateByPrimaryKeySelective(stu);
    }
}
